package com.zhang.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.common.dto.PageSearchDTO;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;
import com.zhang.system.entity.Config;
import com.zhang.system.service.ConfigService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * 系统配置表
 *
 * @author 创世神
 * @since 2024-09-29 11:14:23
 */
@RestController
@RequestMapping("/config")
@Api(tags = "系统配置表控制器")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    /**
     * 列表查询
     *
     * @param searchDTO
     * @return
     */
    @ApiOperation(value = "列表查询", notes = "列表查询", produces = "application/json")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @GetMapping
    public R findList(PageSearchDTO searchDTO) {
        Page<Config> result = configService.findList(searchDTO);
        return RUtils.successData(result);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询", notes = "查询详情")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @GetMapping("/{id}")
    public R findById(@PathVariable("id") Long id) {
        Config config = configService.findById(id);
        return RUtils.successData(config);
    }

    /**
     * 新增
     *
     * @param config
     * @return
     */
    @ApiOperation(value = "新增", notes = "新增数据")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PostMapping
    public R insert(@RequestBody Config config) {
        configService.insert(config);
        return RUtils.successMsg("新增成功");
    }

    /**
     * 修改
     *
     * @param config
     * @return
     */
    @ApiOperation(value = "修改", notes = "修改数据")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PutMapping
    public ResponseEntity<Boolean> update(@RequestBody Config config) {
        boolean result = configService.update(config);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除", notes = "删除数据")
    @DeleteMapping("/{id}")
    public ResponseEntity<Integer> delete(@PathVariable("id") Long id) {
        int result = configService.delete(id);
        return ResponseEntity.ok(result);
    }

}
